/**
 * 
 */
package it.agm.activity.dialog;

import it.agm.fanta.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author alberto
 *
 */
public class GenericDialog {
	
//	public  Context context;
	public  Activity activity;
	public  Context context;
	public Dialog mdialog;
	private Intent intent;
	private String message;
	private String title;
	
	public GenericDialog(Activity activity,String message,String title) {
		super();
		this.activity = activity;
		this.message = message;
		this.title = title;
		createErrorDialogWithActivity();
	}
	
	public GenericDialog( Context context,String message,String title) {
		super();
		this.title = title;
		this.context = context;
		this.message = message;
		createErrorDialogWithContext();
	}

	/**
	 * 
	 */
	private void createErrorDialogWithActivity(){
		
		mdialog = new Dialog(activity);
		mdialog.setContentView(R.layout.dialog_alert_generic);
		TextView mTitle = (TextView) mdialog.findViewById(R.id.alert_message);
		mdialog.setTitle("ERROR");
		String message = "Errore di comunicazione col server";
		if(this.message != null && !"".equals(this.message)){
			message = this.message;
		}
		mdialog.setTitle(this.title);
		mTitle.setText(message);
		View mTitleDivider = mdialog.findViewById(Resources.getSystem().getIdentifier("titleDivider", "id", "android"));
		if(mTitleDivider != null){
			mTitleDivider.setBackgroundColor(activity.getResources().getColor(R.color.white));
		}
		Button mbutton = (Button) mdialog.findViewById(R.id.alert_buttom);
		
		mbutton.setOnClickListener(new OnClickListener() {
	        @Override
			public void onClick(View v) {
	        	if (GenericDialog.this.mdialog != null) {
	        		GenericDialog.this.mdialog.dismiss();
				}
			}
	      });
		mdialog.setCancelable(true);
		mdialog.show();
	}
	
	/**
	 * 
	 */
	private void createErrorDialogWithContext(){
		
		mdialog = new Dialog(context);
		mdialog.setContentView(R.layout.dialog_alert_generic);
		TextView mTitle = (TextView) mdialog.findViewById(R.id.alert_message);
		mdialog.setTitle("ERROR");
		String message = "Errore di comunicazione col server";
		if(this.message != null && !"".equals(this.message)){
			message = this.message;
		}
		mTitle.setText(message);
		View mTitleDivider = mdialog.findViewById(Resources.getSystem().getIdentifier("titleDivider", "id", "android"));
		if(mTitleDivider != null){
			mTitleDivider.setBackgroundColor(context.getResources().getColor(R.color.white));
		}
		Button mbutton = (Button) mdialog.findViewById(R.id.alert_buttom);
		
		mbutton.setOnClickListener(new OnClickListener() {
	        @Override
			public void onClick(View v) {
	        	if (GenericDialog.this.mdialog != null) {
	        		GenericDialog.this.mdialog.dismiss();
				}
			}
	      });
		mdialog.setCancelable(true);
		mdialog.show();
	}
}
